package com.example.demo.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect//定义切面
@Component//让切面随着框架的启动而启动
public class UserAspect {
    //定义切点，@Pointcut注解的参数中定义了具体的拦截规则
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut(){}

    //定义前置通知
    @Before("pointcut()")//表示这个通知是针对pointcut方法的
    public void doBefore(){
        System.out.println("执行了前置通知");
    }

    //定义后置通知
    @After("pointcut()")
    public void doAfter(){
        System.out.println("执行了后置通知");
    }

    //定义环绕通知
    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint){
        System.out.println("环绕通知之前");
        Object result = null;
        try {
            result = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("环绕通知之后");
        return result;
    }
}
